package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.config.VodConstantProperties;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.util.UploadFileUtil;
import com.atguigu.tingshu.vo.album.TrackMediaInfoVo;
import com.baomidou.mybatisplus.extension.service.IService;
import com.qcloud.vod.VodUploadClient;
import com.qcloud.vod.model.VodUploadRequest;
import com.qcloud.vod.model.VodUploadResponse;
import com.tencentcloudapi.common.AbstractModel;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Slf4j
@Service
public class VodServiceImpl implements VodService {

    @Autowired
    private VodConstantProperties vodConstantProperties;
    @Autowired
    private VodUploadClient vodUploadClient;
    @Autowired
    private Credential  credential;

    /**
     * 上传声音
     * @param file
     * @return
     */
    @Override
    public Map<String, String> uploadTrack(MultipartFile file) {
        try {
            //1.将上传文件保存到本地临时目录下 后期采用定时任务清理目录
            String tempPath = UploadFileUtil.uploadTempPath(vodConstantProperties.getTempPath(), file);

            //2.上传文件到云点播平台
            //2.构建文件上传对象
            VodUploadRequest request = new VodUploadRequest();
            request.setMediaFilePath(tempPath);

            //2.2点用上传`
            VodUploadResponse response = vodUploadClient.upload(vodConstantProperties.getRegion(), request);
            if (response !=  null){
                String fileId = response.getFileId();
                String mediaUrl = response.getMediaUrl();
                return Map.of("mediaFileId",fileId,"mediaUrl",mediaUrl);
            }
        } catch (Exception e) {
            log.error("上传声音失败：{}",e.getMessage());
            throw new RuntimeException(e);
        }
        return null;
    }

    /**
     * 获取声音信息
     * @param mediaFileId
     * @return
     */
    @Override
    public TrackMediaInfoVo getMediaInfo(String mediaFileId) {
        try{


            VodClient client = new VodClient(credential, vodConstantProperties.getRegion());
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DescribeMediaInfosRequest req = new DescribeMediaInfosRequest();
            String[] fileIds1 = {mediaFileId};
            req.setFileIds(fileIds1);

            // 返回的resp是一个DescribeMediaInfosResponse的实例，与请求对象对应
            DescribeMediaInfosResponse resp = client.DescribeMediaInfos(req);
            if (resp != null){
                MediaInfo mediaInfo = resp.getMediaInfoSet()[0];
                if (mediaInfo != null){
                    MediaMetaData metaData = mediaInfo.getMetaData();
                    MediaBasicInfo basicInfo = mediaInfo.getBasicInfo();
                    TrackMediaInfoVo trackMediaInfoVo = new TrackMediaInfoVo();
                    trackMediaInfoVo.setType(basicInfo.getType());
                    trackMediaInfoVo.setDuration(metaData.getDuration());
                    trackMediaInfoVo.setSize(metaData.getSize());
                    return trackMediaInfoVo;
                }
            }
        } catch (TencentCloudSDKException e) {
            log.error("获取声音信息失败：{}",e.getMessage());
            throw new RuntimeException(e);
        }
        return null;
    }

    /**
     * 删除声音
     * @param oldFileId
     */
    @Override
    public void deleteMedia(String oldFileId) {
        try{
            // 实例化要请求产品的client对象,clientProfile是可选的
            VodClient client = new VodClient(credential, vodConstantProperties.getRegion());
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DeleteMediaRequest req = new DeleteMediaRequest();
            req.setFileId(oldFileId);
            // 返回的resp是一个DeleteMediaResponse的实例，与请求对象对应
            client.DeleteMedia(req);
        } catch (TencentCloudSDKException e) {
            log.error("删除声音失败：{}",e.getMessage());
        }
    }
}
